#!/usr/bin/env python3
#
# convert the raw output of the selfdocumentation log into a structured JSON file
#

import sys
import json

content = "[" + sys.stdin.read() + '{} ]'  # make an array from the individual dictionary items in the raw txt file and add an empty entry at the end to avoid issues with the trailing comma
database = json.loads(content)

nedtypes = dict()
msgtypes = dict()

for entry in database:
    if entry : # skip empty entries (so you can add an empty entry at the end of the file)
        if "module" in entry:
            nedtypes[entry["module"]] = actions = nedtypes.get(entry["module"], dict())
            actions[entry["action"]] = details = actions.get(entry["action"], list())
            details.append(entry["details"])
        elif "class" in entry:
            msgtypes[entry["class"]] = actions = msgtypes.get(entry["class"], dict())
            actions[entry["action"]] = details = actions.get(entry["action"], list())
            details.append(entry["details"])

print(json.dumps( {"ned" : nedtypes, "msg" : msgtypes}, indent=True))
